package glazedlists.chapter3;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.AdvancedTableModel;
import ca.odell.glazedlists.swing.GlazedListsSwing;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import ca.odell.issuezilla.Issue;
import ca.odell.issuezilla.IssuezillaXMLParser;
import glazedlists.chapter2.IssueComparator;
import glazedlists.chapter2.IssueTableFormat;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.util.Collection;

public class IssuesBrowser3 {

    private EventList<Issue> issueEventList = new BasicEventList<>();

    public IssuesBrowser3(Collection<Issue> issues) {
        issueEventList.addAll(issues);
    }

    public void display() {
        SortedList<Issue> sortedIssues = new SortedList<>(issueEventList, new IssueComparator());
        JTextField filterEdit = new JTextField(10);
        IssueTextFilterator filterator = new IssueTextFilterator();
        MatcherEditor<Issue> matcherEditor = new TextComponentMatcherEditor<Issue>(filterEdit, filterator);
        FilterList<Issue> textFilteredIssues = new FilterList<>(sortedIssues, matcherEditor);

        JPanel panel = new JPanel(new GridBagLayout());
        AdvancedTableModel<Issue> tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(textFilteredIssues,
                new IssueTableFormat());
        JTable issueJTable = new JTable(tableModel);
        TableComparatorChooser.install(issueJTable, sortedIssues, TableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
        JScrollPane issueTableScrollPane = new JScrollPane(issueJTable);
        panel.add(new JLabel("Filter: "), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(filterEdit, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(issueTableScrollPane, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(540, 480);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        final Collection<Issue> issues;
        IssuezillaXMLParser parser = new IssuezillaXMLParser();
        try (InputStream inputStream = IssuesBrowser3.class.getResourceAsStream("/issues.xml")) {
            issues = parser.loadIssues(inputStream, null);
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        SwingUtilities.invokeLater(() -> {
            IssuesBrowser3 issuesBrowser = new IssuesBrowser3(issues);
            issuesBrowser.display();
        });
    }
}
